package com.sxbdjw.situation.activity;

import static android.os.Build.VERSION_CODES.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;



import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends Activity {

    protected void log(String msg) {
        Log.i("BDJWINTERCOM", this.getClass().getSimpleName()+"="+msg);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getViewId());

        initData();
        initView();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        release();
    }

    protected Context getContext() {
        return this;
    }

    protected abstract int getViewId();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract void initEvent();

    protected void release() {
        stopTimer();
        disposable = null;
    }

    protected void onTimer(int ts) {

    }

    Disposable disposable;
    protected void startTimer(int ms) {
        disposable = Observable.interval(ms, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    onTimer(aLong.intValue());
                });
    }

    protected void stopTimer() {
        if (disposable != null) {
            disposable.dispose();
        }
        disposable = null;
    }



    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected void showAlert(String message) {
        new AlertDialog.Builder(this)
                .setTitle("pointer")
                .setMessage(message)
                .setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss(); // 关闭对话框
                    }
                })
                .setNegativeButton("cancel", null)
                .show();
    }



}
